import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from '@/store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home/index'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView,
    children: [{
      path: 'index',
      component: () => import('../views/Index.vue'),
      meta: {
        crumb: ['首页']
      }
    }, {
      path: 'movie-list',
      component: () => import('../views/movie/MovieList.vue'),
      meta: {
        crumb: ['电影管理', '电影列表']
      }
    }, {
      path: 'movie-add',
      component: () => import('../views/movie/MovieAdd.vue'),
      meta: {
        crumb: ['电影管理', '新增电影']
      }
    }, {
      path: 'movie-update/:id',
      component: () => import('../views/movie/MovieUpdate.vue'),
      meta: {
        crumb: ['电影管理', '电影列表:/home/movie-list', '修改电影信息']
      }
    },
    // 动漫
    {
      path: 'dongman-list',
      component: () => import('../views/dongman/dongmanlist.vue'),
      meta: {
        crumb: ['动漫管理', '动漫列表']
      }
    },
    {
      path: 'dongman-add',
      component: () => import('../views/dongman/dongmanAdd.vue'),
      meta: {
        crumb: ['动漫管理', '添加动漫']
      }
    }, {
      path: 'dongman-update/:id',
      component: () => import('../views/dongman/dongmanUpload.vue'),
      meta: {
        crumb: ['动漫管理', '动漫列表:/home/dongman-list', '修改动漫信息']
      }
    },
    // 电视
    {
      path: 'dianshi-list',
      component: () => import('../views/dianshi/dianshilist.vue'),
      meta: {
        crumb: ['电视管理', '电视列表']
      }
    },
    {
      path: 'dianshi-add',
      component: () => import('../views/dianshi/dianshiAdd.vue'),
      meta: {
        crumb: ['电视管理', '添加电视']
      }
    }, {
      path: 'dianshi-update/:id',
      component: () => import('../views/dianshi/dianshiUpload.vue'),
      meta: {
        crumb: ['电视管理', '电视列表:/home/dianshi-list', '修改电视信息']
      }
    },
    // 审核列表
    {
      path: 'shenhe-list',
      component: () => import('../views/shenhe/shenhelist.vue'),
      meta: {
        crumb: ['审核管理']
      }
    },
    ]
  },
  {
    path: '/user/login',
    component: () => import('../views/user/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 注册路由的全局的前置守卫
router.beforeEach((to, form, next) => {
  // console.log('从哪来', form);
  // console.log('到哪去', to);
  // 在此处判断, 是否是去往登录页面?
  if (to.path == '/user/login') {
    next()
  } else {
    // 判断, vuex中是否有当前登录用户, 如果有,则继续
    if (store.state.user) {
      next()
    } else {
      // 若没有, 则跳转到登录页面
      router.push('/user/login')
    }
  }
})

export default router
